<?php


namespace common\models\service;


use common\cache\DictCache;
use common\helpers\commonApi;
use common\models\Campus;
use common\models\Running;
use common\models\User;
use yii\helpers\ArrayHelper;

class RunningService extends Running
{
    /**
     * 列表
     * @param $campus_id 校区
     * @param $where
     * @return array|string
     */
    public static function getList($campus_id,$where=[])
    {
        $ba = new BaseController();
        $ba->model = Running::className();
        if ($campus_id){
            $where['campus_id'] = $campus_id;
        }
        $data = $ba->actionIndex(true,$where);
        $campus_ids = array_column($data['data'],'campus_id');
        $campus = (new Campus())->getCampusName($campus_ids);
        $userids = array_unique(ArrayHelper::getColumn($data['data'], 'userid'));
        $userInfo = (new User())->getUserHtml($userids);
        $sex = DictCache::get('astrict_sex');
        foreach ($data['data'] as &$datum){
            $datum['campus'] = $campus[$datum['campus_id']]??'';
            $datum['sex'] = $sex[$datum['sex']]??'';
            $datum['user_html'] = $userInfo[$datum['userid']]??'';
            $datum['id_card'] = $datum['id_card_front'].','.$datum['id_card_other'];
        }
        return $data;
    }

    /**
     * 审核
     * @param $post
     * @return array
     */
    public static function review($post)
    {
        if (\Yii::$app->request->isGet){
            $id = \Yii::$app->request->get('id','');
            $model = Running::findOne($id);
            if (!$model){
                $back = new BaseController();
                return $back->bError('参数错误');
            }
            $astrict_sex = DictCache::get('astrict_sex');
            $review_status = DictCache::get('review_status');
            $info = $model->toArray();
            $campus = (new Campus())->getCampusName([$info['campus_id']]);
            $info['campus'] = $campus[$info['campus_id']]??'';
            $info['sex'] = $astrict_sex[$info['sex']]??'';
            unset($review_status[0]);
            return ['model'=>$info,'review_status'=>$review_status];
        }
        if (empty($post['id'])){
            return commonApi::message('请选择要审核的人员',false);
        }
        if (empty($post['review_status'])){
            return commonApi::message('请选择审核状态',false);
        }
        $model = self::findOne($post['id']);
        if ($model->review_status!==0){
            return commonApi::message('请勿重复审核',false);
        }
        if ($post['review_status']==1){
            $model->status = 1;
        }
        $model->review_status = $post['review_status'];
        $model->review_time = date('Y-m-d H:i:s');
        $model->review_remarks = $post['review_remarks']??'';
        if ($model->save()){
            return commonApi::message('审核成功');
        }
        return commonApi::message('审核失败',false);
    }

    /**
     * 统计
     * @param $start_time
     * @param $end_time
     * @param $campus_id
     * @return int[]
     */
    public static function statistics($start_time,$end_time,$campus_id)
    {
        $where = [];
        if ($start_time&&$end_time){
            $where = ['between','create_time',$start_time,$end_time];
        }
        $running = Running::find()->where($where)->andWhere(['review_status'=>1])
            ->andFilterWhere(['campus_id'=>$campus_id])
            ->select(['sum(total_brokerage) as amount','count(*) as num'])
            ->asArray()->one();
        $amount = $running['amount']??0;
        $num = $running['num']??0;
        return ['amount'=>$amount,'num'=>$num];
    }
}